'''
Back end for the coffee bar workflow (Winter Intensives Lab 5)

This file is where you "wire" together the steps in the process.
'''

# these next few lines import some of the WMP functions we will
# use in this file.
from backend.backend import Backend
from workflow.task import Task
# from workflow.result import Result # uncomment this line if using Result objects (not needed in simple workflow)
from workflow.flowData import Status

class CoffeeBackend(Backend):
    '''
    The CoffeeBackend "class" is a collection of the "methods" (functions) that do the actual
    wiring together of the steps in the coffee bar process.
    
    A back end will always include the methods __init__ and wire and will also include a method
    to handle each event that occurs in the process.  For Lab 5 we have set up the two additional
    methods you need:  drink_order_taken and drink_prepared.
    
    See the notes for each method to see what you need to do.
    '''
   
    def __init__(self): 
        '''
        The __init__ method is called automatically when we run the code in this file.
        
        The __init__ method, like all methods, has the first parameter 'self'.
        This is a Python requirement for methods (any function that is part of a 
        Class definition).  You should always leave this parameter alone.
        
        In this case, __init__ for our CoffeeBackend class calls the __init__
        method of another class Backend which is part of the WMP system.
        
        Backend.__init__ has two parameters.  The first, is again self and
        you should, as always, leave it alone.
        
        The second parameter here is a "string" and should be the name of your workflow.
        This is going to also be the name given to the spreadsheet which MWP will use to 
        store all data for this workflow.
        
        You can call your workflow whatever you like BUT:
        - the name should be meaningful (not "workflow 23")
        - the name should be different than all other workflows in your
        organization (i.e. sharing the same Google account)
        - you have to use the same name in all the other "front end" Python files, since
        the name of the workflow is how MWP knows which tasks are sharing information
        in the same spreadsheet.
        
        For Lab 5 we need to make sure that each student uses a different name for the
        workflow to avoid two students trying to store data in the same spreadsheet, so:
        
        The workflow name should be your name plus the word coffee.  For example, if your
        name is Rhett Terrier, then your workflow name would be 'RhettTerrierCoffee'.
        '''
        Backend.__init__(self, "WorkflowName")  # be sure you update the workflow name

    def wire(self):
        '''
        The wire method is where we tell MWP which tasks to keep track of.
        
        wire does not take any parameters other than self, but you will need to 
        add a line of code for each task in the process that MWP needs to keep
        track of.
        
        For Lab 5 there are two tasks we need to 'register':  taking an order
        and preparing the drink.
        
        To register a task, you will need to add a line of code that looks
        like this:
        
        self.register_result_listener("RoleName", "TaskName", self.method_name)
        
        In this line of code:
        
        self.register_result_listener is the name of the function being called.
        
        (The actual code for this can be found in a file called backend.py which
        you don't need to change or even look at, since it is part of the MWP
        system.  If you look above, you will see that this code is imported 
        at the top of this file.)
        
        The first parameter "RoleName" should be replaced with the name of the 
        role (aka person aka job) that carries out this task.  For example, "Barista"
        if this is a task that the Barista does.  the RoleName should be all letters
        and numbers with NO SPACES.  So if the task were being carried out by a
        computer programmer you would use a rolename like "ComputerProgrammer" (or "Programmer").
        
        "TaskName" should be the name you give to this particular step in the process.
        Again, this name should have no spaces in it.
        
        Note that you are free to use whatever names you like for the role and the
        task, but you need to be consistent throughout the MWP application.
        In particular, when you define your user interface for a given task (e.g.
        in BaristaApplication.py and OrderTakerApplication.py) you will need to 
        register those tasks using the same role and task names you use here.
        
        Finally, self.method_name should be the name of the method you will use (below)
        to take action after this task has been carried out.  Typically the action
        will be to assign the next task in the process.  Note that the method name
        is prefixed by 'self.' and that there are no quotes around self.method_name.
        
        For Lab 5 we have already set up the two methods you will need here:
        
        self.drink_order_taken
        self.drink_prepared
        
        so you will use one of these in each line of code.
        '''
        # add a line of code here to register the step where a drink order is taken.
        # add a line of code here to register the step where a drink is prepared.

    def drink_order_taken(self, results):
        '''
        this method (i.e. function) gets called to update data and schedule any tasks
        necessary after a drink order is taken.  specifically, this function
        will set the status to "complete" on the "take drink order" task
        and assigns the preparation of this drink to the Barista.
        
        you don't call this function yourself.  MWP will call the function
        whenever there is a new row in the Google spreadsheet for a drink order.
        you tell MWP to call this particular function for this particular
        event by adding a line of code in the wire method above.
        
        when drink_order_taken gets called, the results parameter will be
        set by WPM to contain a list of "results" from any recent
        drink orders that need to be dealt with.  each result "object"
        contains all the data associated with that particular drink order.
        
        in what follows we already include the code to update the status
        of the "take drink order" task.  we also include the line of code
        which adds the new task to the workflow system.  what is NOT included
        below is the code to create the new task to be added.  you need to 
        add a line of code that looks like this:
        
        task = Task.construct_from_result(result, "RoleName", "TaskName")
        
        in this code, RoleName and TaskName have the same meaning as in the 
        code you added to the wire() method above.
        '''
        for result in results:  # repeat the following actions for each result
            task = 'you need to replace this text in quotes with the right code ' 
            self.workflow.add(task) # add the new task to the workflow
            self.workflow.update_status(result, Status.COMPLETE)

    def drink_prepared(self, results):
        '''
        this method is similar to drink_order_taken, except that it is called
        after the barista has prepared the drink.  in our very simple workflow
        this is the last step in the process, which means that this method has
        a very simple job:  it just needs to mark the status of the barista's
        task (stored in the results variable) as Complete.  You do not need
        to create a new task and you do not need to add a task to the workflow.
        
        we have not included the code for this method below.  the "pass" statement
        below tells python to "pass" on doing anything at all in this method.
        
        to complete the code for this method you will want to remove the pass
        statement and replace it with some of the code from drink__order_taken.
        Try copying all the code and deleting whatever you don't need here.
        '''
        pass

'''
finally, this last bit of code is fine as it is and you do not (and
should not) change it.

what this code does is initialize the backend by running the __init__ method
of the CoffeeBackend class (above).

the peculiar if statement is there to be sure the this code only runs if you
really mean to run it and not (say) if you are importing this code to use
it in another python module.  strange stuff, feel free to ignore it.
'''
if __name__ == '__main__':
    backend = CoffeeBackend()